package com.woniuxy.crm.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.crm.entity.Manager;
import com.woniuxy.crm.result.Result;
import com.woniuxy.crm.service.IManagerService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 管理员or销售表 前端控制器
 *
 * @author 郑鹏
 * @since 2024-08-12
 */
@RestController
@RequestMapping("/admin/manager")
@Tag(name = "管理员or销售表接口")
public class ManagerController {

    @Autowired
    private IManagerService managerService;

    // 新增或者更新
    @PostMapping("/saveOrUpdate")
    @Operation(summary = "新增或者修改")
    public Result save(@RequestBody Manager manager) {
        managerService.saveOrUpdate(manager);
        return Result.ok();
    }

    @DeleteMapping("/deleteById")
    @Operation(summary = "根据id删除")
    public Result delete(@RequestParam Long id) {
        managerService.removeById(id);
        return Result.ok();
    }

//    @PostMapping("/del/batch")
//    @Operation(summary = "批量删除")
//    public boolean deleteBatch(@RequestBody List<Integer> ids) {
//        return managerService.removeByIds(ids);
//    }

    @GetMapping("list")
    @Operation(summary = "查询全部")
    public Result<List<Manager>> findAll() {
        LambdaQueryWrapper<Manager> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Manager::getRole, 1);

        List<Manager> managers = managerService.list(queryWrapper);
        return Result.ok(managers);
    }

    @GetMapping("getById")
    @Operation(summary = "根据id查询")
    public Result<Manager> findOne(@RequestParam Long id) {
        LambdaQueryWrapper<Manager> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Manager::getRole, 1);
        queryWrapper.eq(Manager::getId, id);

        Manager manager = managerService.getOne(queryWrapper);
        return Result.ok(manager);
    }

    @GetMapping("/page")
    @Operation(summary = "分页查询")
    public Result<Page<Manager>> findPage(@RequestParam Integer pageNum, @RequestParam Integer pageSize) {
        LambdaQueryWrapper<Manager> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByAsc(Manager::getId);
        queryWrapper.eq(Manager::getRole, 1);
        Page<Manager> page = managerService.page(new Page<>(pageNum, pageSize), queryWrapper);
        return Result.ok(page);

    }

}

